import {
  DEFAULT_COLOR_CASE,
  DEFAULT_FUNCTION_CFG,
  DEFAULT_LABEL,
  DEFAULT_LEGEND_STYLE,
  DEFAULT_SIZE,
  DEFAULT_SPLIT,
  DEFAULT_THRESHOLD,
  DEFAULT_TITLE_STYLE,
  DEFAULT_TOOLTIP,
  DEFAULT_TOTAL,
  DEFAULT_XAXIS_STYLE,
  DEFAULT_YAXIS_EXT_STYLE,
  DEFAULT_YAXIS_STYLE,
} from '@/components/vis/chart/chart/chart'
import { adaptCurTheme } from '@/components/canvas/utils/style'

function setChartDefaultOptions(view) {
  const type = view.type
  const attr = JSON.parse(view.customAttr)
  if (view.render === 'echarts') {
    attr.label.position = 'inside'
  } else {
    attr.label.position = 'middle'
  }
  if (type.includes('pie')) {
    if (view.render === 'echarts') {
      attr.label.position = 'inside'
    } else {
      const customStyle = JSON.parse(view.customStyle)
      customStyle.legend.show = false
      view.customStyle = JSON.stringify(customStyle)
      attr.label.show = true
      attr.label.position = 'outer'
    }
    if (type === 'pie-donut') {
      attr.size.pieInnerRadius = Math.round(attr.size.pieOuterRadius * 0.75)
    }
    if (type === 'pie-donut-rose') {
      attr.size.pieInnerRadius = Math.round(attr.size.pieOuterRadius * 0.5)
    }
  } else if (type.includes('bar')) {
    attr.label.labelContent = ['quota']
    const senior = JSON.parse(view.senior)
    senior.functionCfg.emptyDataStrategy = 'ignoreData'
    view.senior = JSON.stringify(senior)
  } else if (type.includes('line')) {
    attr.label.position = 'top'
  }
  view.customAttr = JSON.stringify(attr)
}

export function getNewView(params) {
  const view = {}
  view.name = params.name
  view.title = params.name
  view.sceneId = params.panelId
  view.tableId = params.tableId
  view.type = params.type || 'line'
  view.isPlugin = false
  view.render = 'echarts'
  view.resultMode = 'custom'
  view.resultCount = 1000
  view.refreshViewEnable = false
  view.refreshUnit = 'minute'
  view.refreshTime = 5
  const customAttr = {
    color: DEFAULT_COLOR_CASE,
    tableColor: DEFAULT_COLOR_CASE,
    size: DEFAULT_SIZE,
    label: DEFAULT_LABEL,
    tooltip: DEFAULT_TOOLTIP,
    totalCfg: DEFAULT_TOTAL,
  }
  const customStyle = {
    text: DEFAULT_TITLE_STYLE,
    legend: DEFAULT_LEGEND_STYLE,
    xAxis: DEFAULT_XAXIS_STYLE,
    yAxis: DEFAULT_YAXIS_STYLE,
    yAxisExt: DEFAULT_YAXIS_EXT_STYLE,
    split: DEFAULT_SPLIT,
  }
  // 新建的视图应用当前主题
  adaptCurTheme(customStyle, customAttr)
  view.customAttr = JSON.stringify(customAttr)
  view.customStyle = JSON.stringify(customStyle)
  view.senior = JSON.stringify({
    functionCfg: DEFAULT_FUNCTION_CFG,
    assistLine: [],
    threshold: DEFAULT_THRESHOLD,
  })
  view.stylePriority = 'view' // 默认样式优先级视图
  view.xaxis = JSON.stringify(params.xaxis || [])
  view.xaxisExt = JSON.stringify([])
  view.yaxis = JSON.stringify(params.yaxis || [])
  view.yaxisExt = JSON.stringify([])
  view.extStack = JSON.stringify([])
  view.customFilter = JSON.stringify(params.filters || [])
  view.drillFields = JSON.stringify([])
  view.extBubble = JSON.stringify([])
  view.viewFields = JSON.stringify([])
  setChartDefaultOptions(view)

  return view
}
